<?php if(!class_exists("View", false)) exit("no direct access allowed");?><main id="main" class="in">
    <style>
      .top{text-align: end;padding-top: 10px;}
      .top_controller{margin: 5px;}
    </style>
    
      <div class="container-fluid">
        <div class="row">
            <div class="col-xs-3">
                <div class="panel">
                    <div class="panel-heading">
                      <div class="panel-title">维护部门</div>
                    </div>
                    <div class="panel-body">
                      <ul data-name="tree-dept" id="deptTree" class="tree" data-idx="0"></ul>
                    </div>
                </div>
            </div>
            <div class="col-xs-9">
              <div class="panel">                    
                <div class="panel">
                  <div class="panel-heading">
                    <div class="panel-title">下级部门</div>
                  </div>
                  <div class="panel-body">
                    <form method="post" target="hiddenwin" action="<?php echo url(array('c'=>"System", 'a'=>"manageChild", ));?>>
                      <div class="row">
                        <div class="col-xs-6 top">
                          <nobr>
                            <a href="#">人本智汇-凤凰FM</a>
                            <i class="icon-angle-right"></i>
                            <?php if(!empty($parentDepts)){ $_foreach_v_counter = 0; $_foreach_v_total = count($parentDepts);?><?php foreach( $parentDepts as $k => $v ) : ?><?php $_foreach_v_index = $_foreach_v_counter;$_foreach_v_iteration = $_foreach_v_counter + 1;$_foreach_v_first = ($_foreach_v_counter == 0);$_foreach_v_last = ($_foreach_v_counter == $_foreach_v_total - 1);$_foreach_v_counter++;?>
                            <a href="<?php echo url(array('c'=>"System", 'a'=>"DepartmentBrowse", 'id'=>"", ));?>"><?php echo htmlspecialchars($v['name'], ENT_QUOTES, "UTF-8"); ?></a>
                            <i class="icon-angle-right"></i> 
                            <?php endforeach; }?>                            
                        </nobr>
                        </div>
                        <div class="col-xs-6">
                          <?php if(!empty($sons)){ $_foreach_v_counter = 0; $_foreach_v_total = count($sons);?><?php foreach( $sons as $k => $v ) : ?><?php $_foreach_v_index = $_foreach_v_counter;$_foreach_v_iteration = $_foreach_v_counter + 1;$_foreach_v_first = ($_foreach_v_counter == 0);$_foreach_v_last = ($_foreach_v_counter == $_foreach_v_total - 1);$_foreach_v_counter++;?>
                              <input type="text" name="depts" data-id="<?php echo htmlspecialchars($v['id'], ENT_QUOTES, "UTF-8"); ?>" value="<?php echo htmlspecialchars($v['name'], ENT_QUOTES, "UTF-8"); ?>" class="form-control top_controller" autocomplete="off">
                              <?php endforeach; }?> 
                              <input type="text" name="depts" data-id="0" value="" class="form-control top_controller" autocomplete="off">
                              <input type="text" name="depts" data-id="0" value="" class="form-control top_controller" autocomplete="off">
                              <input type="text" name="depts" data-id="0" value="" class="form-control top_controller" autocomplete="off">
                              <input type="text" name="depts" data-id="0" value="" class="form-control top_controller" autocomplete="off">
                              <input type="text" name="depts" data-id="0" value="" class="form-control top_controller" autocomplete="off">
                              <input type="text" name="depts" data-id="0" value="" class="form-control top_controller" autocomplete="off">
                        </div>
                      </div>
                      <div class="row" style="margin:10px">
                        <div class="col-xs-12 top">
                          <button type="button" id="submit" class="btn btn-wide btn-primary"
                                data-loading="稍候...">保存</button> 
                                <a href="javascript:history.go(-1);"
                                class="btn btn-back btn-wide">返回</a>
                                <input type="hidden" name="parentDeptID" id="parentDeptID" value="<?php echo htmlspecialchars($deptID, ENT_QUOTES, "UTF-8"); ?>">
                        </div>
                      </div>
                    </form>
                  </div>
                </div>
              </div>
            </div>
        </div>      
      </div>
      <input type="hidden" id="dept" value='<?php echo htmlspecialchars($dept, ENT_QUOTES, "UTF-8"); ?>'>
      <script src="http://p1.renbenzhihui.com/zui/js/all.js"></script>
      <script src="/i/js/util.js"></script>
      <script>      
        $(function()
        {
            var data = JSON.parse($('#dept').val());
            var options = {
                name: 'deptTree',
                initialState: 'preserve',
                data: data,
                sortable: 
                {
                    lazy: true,
                    nested: true,
                    canMoveHere: function($ele, $target)
                    {
                        if($ele && $target && $ele.parent().closest('li').attr('data-id') !== $target.parent().closest('li').attr('data-id')) return false;
                    }
                },
                itemCreator: function($li, item)
                {
                    var link = item.id !== undefined ? ('<a href="<?php echo url(array('c'=>"System", 'a'=>"DepartmentBrowse", 'id'=>"", ));?>'+item.id+'">' + item.name + '</a>') : ('<span class="tree-toggle">' + item.name + '</span>');
                    var $toggle = $('<span class="dept-name module-name" data-id="' + item.id + '">' + link + '</span>');
                    if(item.manager)
                    {
                        $toggle.append('&nbsp; <span class="dept-manager text-muted"><i class="icon icon-user"></i> ' + item.managerName + '</span>');
                    }
                    $li.append($toggle);
                    return true;
                },
                actions: 
                {
                    sort:
                    {
                        title: '拖动排序',
                        template: '<a class="sort-handler"><i class="icon-move"></i></a>'
                    },
                    "delete":
                    {
                        linkTemplate: '<?php echo url(array('c'=>"System", 'a'=>"DepartmentDel", 'id'=>"", ));?>{0}',
                        title: '删除部门',
                        template: '<a><i class="icon-trash"></i></a>'
                    }
                },
                action: function(event)
                {
                    var action = event.action, $target = $(event.target), item = event.item;
                    if(action.type === 'edit')
                    {
                        new $.zui.ModalTrigger({
                            type: 'ajax',
                            url: action.linkTemplate.format(item.id),
                            keyboard: true
                        }).show();
                    }
                    else if(action.type === 'delete')
                    {
                        //window.open(action.linkTemplate.format(item.id), 'hiddenwin');
                        $.get(action.linkTemplate.format(item.id),function(data){
                          data = JSON.parse(data);
                          if(data.code/1 == 1){
                            location.reload();
                          }else{
                            new $.zui.Messager(data.msg, {
                                type: 'danger',
                                icon: 'exclamation-sign',
                                placement: 'center' // 定义显示位置
                            }).show();
                          }
                        });
                    }
                    else if(action.type === 'sort')
                    {
                        var orders = new Array();
                        $('#deptTree').find('li:not(.tree-action-item)').each(function()
                        {
                            var $li = $(this);
                            var item = $li.data();
                            var id=item.id;                            
                            var or = item.order;
                            orders.push({'id':id,'order':or});
                            //orders['orders[' + item.id + ']'] = $li.attr('data-order') || item.order;
                        });
                        $.post('<?php echo url(array('c'=>"System", 'a'=>"DepartmentUpdate", ));?>', {'ordata':orders}).error(function()
                        {
                            bootbox.alert(lang.timeout);
                        });
                    }
                }
            };
        
            if(false) options.actions["sort"] = false;
            if(false) options.actions["edit"] = false;
            if(false) options.actions["delete"] = false;
        
            var $tree = $('#deptTree').tree(options);
        
            var tree = $tree.data('zui.tree');
            if(!tree.store.time) tree.expand($tree.find('li:not(.tree-action-item)').first());
        
            $tree.on('mouseenter', 'li:not(.tree-action-item)', function(e)
            {
                $('#deptTree').find('li.hover').removeClass('hover');
                $(this).addClass('hover');
                e.stopPropagation();
            });
            $('#submit').on('click',function(){
              var parentDeptID= $('#parentDeptID').val();
              var depts = new Array();
              $('input[name="depts"]').each(function(){
                var id = Utils.paseData($(this).attr("data-id"));
                var name = Utils.paseData($(this).val());
                if(name != ''){
                  depts.push({'id':id,'name':name});
                }                
              });
              $.post('<?php echo url(array('c'=>"System", 'a'=>"ManageChild", ));?>',{parentDeptID:parentDeptID,depts:depts},function(data){
                data = JSON.parse(data);
                if(data.code/1 == 1){
                  location.reload();
                }else{
                  new $.zui.Messager(data.msg, {
                      type: 'danger',
                      icon: 'exclamation-sign',
                      placement: 'center' // 定义显示位置
                  }).show();
                }
              });
            });
        });
        </script>
  </main>